package fabworkz.anna;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
//import android.widget.ArrayAdapter;

public class VoiceRecognition extends Activity {

	private static int REQUEST_CODE=1010;
	   // private ListView wordsList;
	private SpeechRecognizer sr;
	    /**
	     * Called with the activity is first created.
	     */
	    @Override
	    public void onCreate(Bundle savedInstanceState)
	    {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_voice_recognition);
	        Log.d("ANNA", "voice recognition class created");
//	        sr = SpeechRecognizer.createSpeechRecognizer(this);       
//            sr.setRecognitionListener(new listener()); 
	 
	        //wordsList = (ListView) findViewById(R.id.list);
	 
	        // Disable button if no recognition service is present
//	        PackageManager pm = getPackageManager();
//	        List<ResolveInfo> activities = pm.queryIntentActivities(
//	                new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
//	        if (activities.size() != 0)
//	        {
	        	startVoiceRecognitionActivity();
	        	//finish();
//	        }
	    }
	 
	    /**
	     * Handle the action of the button being clicked
	     */
	 
	    /**
	     * Fire an intent to start the voice recognition activity.
	     */
	    private void startVoiceRecognitionActivity()
	    {
	    	Log.d("ANNA", "Starting voice recognition activity");
	        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
	        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
	                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
	        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Listening...");
	        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
	        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"fabworkz.anna");
	        startActivityForResult(intent, REQUEST_CODE);
	        //sr.startListening(intent);
	    }
	 
	    /**
	     * Handle the results from the voice recognition activity.
	     */
	    @Override
	    protected void onActivityResult(int requestCode, int resultCode, Intent data)
	    {
	        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
	        {
	            // Populate the wordsList with the String values the recognition engine thought it heard
	            ArrayList<String> matches = data.getStringArrayListExtra(
	                    RecognizerIntent.EXTRA_RESULTS);
	            if(matches.get(0).equals("Google")){
	            	Log.d("ANNA", "heard Google, launching voice search..");
	            	Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
	            	intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	            	startActivity(intent);
	            }
	            else if(matches.get(0).equals("Computer")){
	            	Log.d("ANNA","heard computer, listening for more..");
	    	        startVoiceRecognitionActivity();
	            }
	            else if(matches.get(0).equals("stop")){
	            	Log.d("ANNA","heard stop, finishing up..");
	            }
	            
	            //wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
	            //        matches));
	        }
	        super.onActivityResult(requestCode, resultCode, data);
	    }
	    
	    class listener implements RecognitionListener          
	    {
	             private static final String TAG = "ANNA RecognitionListener";
				public void onReadyForSpeech(Bundle params)
	             {
	                      Log.d(TAG, "onReadyForSpeech");
	             }
	             public void onBeginningOfSpeech()
	             {
	                      Log.d(TAG, "onBeginningOfSpeech");
	             }
	             public void onRmsChanged(float rmsdB)
	             {
	                      Log.d(TAG, "onRmsChanged");
	             }
	             public void onBufferReceived(byte[] buffer)
	             {
	                      Log.d(TAG, "onBufferReceived");
	             }
	             public void onEndOfSpeech()
	             {
	                      Log.d(TAG, "onEndofSpeech");
	             }
	             public void onError(int error)
	             {
	                      Log.d(TAG,  "error " +  error);
	             }
	             public void onResults(Bundle results)                   
	             {
	                      Log.d(TAG, "onResults " + results);
	                      ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
	                      
	                      if(matches.get(0).equals("Google")){
	      	            	Log.d("ANNA", "heard Google, launching voice search..");
	      	            	Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
	      	            	startActivity(intent);
	      	            	finish();
	      	            }
	      	            else if(matches.get(0).equals("Computer")){
	      	            	Log.d("ANNA", "heard Computer, waiting for more..");
	      	    	        //startVoiceRecognitionActivity();
	      	            }
	      	            else if(matches.get(0).equals("stop")){

		                      finish();
	      	            }
	             }
	             public void onPartialResults(Bundle partialResults)
	             {
	                      Log.d(TAG, "onPartialResults");
	             }
	             public void onEvent(int eventType, Bundle params)
	             {
	                      Log.d(TAG, "onEvent " + eventType);
	             }
	    }
//	    public void finish(String command) {
//	    	  // Prepare data intent 
//	    	  Intent data = new Intent();
//	    	  data.putExtra("returnKey1", command);
//	    	  
//	    	  /*
//	    	  data.putExtra("returnKey2", "You could be better then you are. ");*/
//	    	  // Activity finished ok, return the data
//	    	  setResult(RESULT_OK, data);
//	    	  super.finish();
//	    	} 

}
